<?php
class ModelMyplanPlan extends Model{
	public function addPlan($data){
		$this->db->query("insert into myplan_plan set user_id='".$this->db->escape($data['user_id'])."',type_id='".$this->db->escape($data['type_id'])."',content='".$this->db->escape($data['content'])."',date_added=NOW()");
		
		$plan_id=$this->db->getLastId();
		
		$plan=$this->getPlan($plan_id);
		
		$this->db->query("insert into myplan_friendsnews set news_type='1',user_id='".$this->db->escape($data['user_id'])."',content='".$this->db->escape($plan['content'])."',params='".$this->db->escape("&user_id=").$data['user_id'].$this->db->escape("&plan_id=".$plan_id)."',date_added=NOW()");
	}
	
	public function getALLPlans($data){
		$sql="select * from myplan_plan p,myplan_plan_type t where user_id='".$data["user_id"]."' and p.type_id=t.type_id ";
		
		if(isset($data["status"])){
			$sql.=" and status='".$data["status"]."'";
		}
		
		if (isset($data['start']) || isset($data['limit'])) {
				if ($data['start'] < 0) {
					$data['start'] = 0;
				}				

				if ($data['limit'] < 1) {
					$data['limit'] = 20;
				}	
			
				$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
		}
		
		return $this->db->query($sql)->rows;
	}
	
	public function getTotalPlans($data){
		$sql="select COUNT(*) as total from myplan_plan p,myplan_plan_type t where user_id='".$data["user_id"]."' and p.type_id=t.type_id ";
		
		if(isset($data["status"])){
			$sql.=" and status='".$data["status"]."'";
		}
		
		return $this->db->query($sql)->row["total"];
	}
	
	public function getPlan($plan_id){
		return $this->db->query("select * from myplan_plan  where plan_id='".$plan_id."'")->row;
	}
	
	public function updatePlan($data){
		$sql="update myplan_plan set status='".$data["action"]."' ";
		
		if($data["action"]==4||$data["action"]==5){
			$sql.=",date_complete='".date("Y-m-d")."'";
		}
		
		$sql.=" where plan_id='".$data["plan_id"]."' and user_id='".$data["user_id"]."'";
		
		$this->db->query($sql);
	}
	
	public function getStatus($status){
		return $this->db->query("select * from myplan_plan_status where status='".$status."' order by status")->row;
	}
	
	public function getAllStatus(){
		return $this->db->query("select * from myplan_plan_status order by status")->rows;
	}
	
	public function getPlanTypes(){
		return $this->db->query("select * from myplan_plan_type order by type_id")->rows;
	}
	
	public function getPlanCheckIn($plan_id){
		$temp=$this->db->query("select * from myplan_plan_checkin where plan_id='".$plan_id."' order by date_checkin")->rows;
		$plancheckin=array();
		foreach($temp as $checkin){
			$checkin["date_checkin"]=substr($checkin["date_checkin"],0,10);
			$plancheckin[]=$checkin;
		}
		return $plancheckin;
	}
	
	public function addCheckIn($data){
		$this->db->query("insert into myplan_plan_checkin set plan_id='".$this->db->escape($data['plan_id'])."',checkin_note='".
						$this->db->escape($data['checkin_note'])."',date_checkin='".date("Y-m-d")."'");
	}
	
	public function addNote($data){
		$this->db->query("insert into myplan_plan_note set plan_id='".$this->db->escape($data['plan_id'])."',note_content='".
						$this->db->escape($data['note_content'])."',date_added=NOW() ");
	}
	
	public function addComment($data){
		$this->db->query("insert into myplan_plan_comment set plan_id='".$this->db->escape($data['plan_id'])."',from_user_id='".$this->db->escape($data['from_user_id'])."',comment_content='".
						$this->db->escape($data['comment_content'])."',date_added=NOW() ");
						
		//info type is 1 means you get a comment.				
		$this->db->query("insert into myplan_information set user_id='".$data["user_id"]."',content='你有新的评论',params='plan_id=".$data["plan_id"]."',info_type='1'");
	}
	
	public function getPlanNote($plan_id){
		return $this->db->query("select * from myplan_plan_note  where plan_id='".$plan_id."'")->rows;
	}
	
	public function getPlanComment($plan_id){
		return $this->db->query("select c.plan_id,u.user_id,u.nickname,c.comment_content,c.date_added from myplan_plan_comment c,myplan_user u  where c.from_user_id=u.user_id and c.plan_id='".$plan_id."'")->rows;
	}
}
?>
